﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication15
{
   public partial class Form1 : Form
    {
        #region "Biến cục bộ trong Form"
            private OleDbConnection objConnect;
            private DataTable objDataTable;
            private enum varMode : int { AddMode, EditMode, DeleteMode }
        #endregion
        #region "Tạo Kết nối và hủy kết nối"
            /// <summary>
            /// Tạo một Connection
            /// </summary>
            private void subCreateConnect()
            {
                String varChuoiConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ;" +
                            "Data Source =" + Application.StartupPath + @"\invoice.mdb;" +
                            "Jet OLEDB:Database Password = pvlld1012;" +
                            "User ID = admin";
                objConnect = new OleDbConnection(varChuoiConnect);
                objConnect.Open(); //Mở kết nối
            }
            /// <summary>
            /// Hủy kết nối
            /// </summary>
            private void subDestroyConnect()
            {
                objConnect.Close(); //Đóng kết nối
                objConnect.Dispose();//Giải phóng tài nguyên
                objConnect = null; //Hủy đối tượng
            }
        #endregion   
       public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
                subCreateConnect();
                OleDbCommand objcommand = new OleDbCommand("Select * from txtPayer", objConnect);
                OleDbDataReader objReader = objcommand.ExecuteReader();
                string[,] array = new string[3, 5];
                int b = 0;
                while (objReader.Read())
                {
                    array[0, b] = objReader["PayerName"].ToString();
                    array[1, b] = objReader["PayerVatCode"].ToString();
                    array[2, b] = objReader["PayerAdd"].ToString();
                    b++;
                }
                comboBox1.Items.Clear();
                for (int i = 0; i < array.Length / 3; i++)
                {
                    comboBox1.Items.Add(array[0, i]);
                }
                subDestroyConnect();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            subCreateConnect();
            OleDbCommand objcommand = new OleDbCommand("Select * from txtPayer", objConnect);
            OleDbDataReader objReader = objcommand.ExecuteReader();
            HtmlElement txtPayer_text = webBrowser1.Document.GetElementById("txtPayer_text");
            HtmlElement txtVatCode_text = webBrowser1.Document.GetElementById("txtVatCode_text");
            HtmlElement txtAddress_text = webBrowser1.Document.GetElementById("txtAddress_text");
            string[,] array = new string[3, 5];
            int b = 0;
            while (objReader.Read())
            {
                array[0, b] = objReader["PayerName"].ToString();
                array[1, b] = objReader["PayerVatCode"].ToString();
                array[2, b] = objReader["PayerAdd"].ToString();
                b++;
            }
            int idcombo = comboBox1.SelectedIndex;
            txtPayer_text.SetAttribute("value", array[0, idcombo]);
            txtVatCode_text.SetAttribute("value", array[1, idcombo]);
            txtAddress_text.SetAttribute("value", array[2, idcombo]);
        }
        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
